<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>原型链属性检查器</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/default.min.css">
    <style>
        /* 基本重置 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 500px;
        }

        h1 {
            text-align: center;
            font-size: 1.5rem;
            margin-bottom: 20px;
        }

        .input-section {
            margin-bottom: 20px;
            display: flex;
            flex-direction: column;
        }

        label,
        select,
        input {
            margin-bottom: 10px;
            font-size: 1rem;
        }

        input,
        textarea {
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        button {
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .result {
            margin-top: 20px;
            padding: 10px;
            font-size: 1.1rem;
            text-align: center;
            border-radius: 4px;
        }

        .result.success {
            background-color: #d4edda;
            color: #155724;
        }

        .result.error {
            background-color: #f8d7da;
            color: #721c24;
        }

        .icon {
            margin-right: 8px;
        }

        .result .icon-success {
            color: #28a745;
        }

        .result .icon-error {
            color: #dc3545;
        }
        .object-example-container {
            outline: none;
            padding: 20px 30px;
            background-color: #fff;
            color: #535455;
            border: 1px solid #ded4d5;
            border-radius: 8px;
        }
        .object-example-container:focus {
            border-color: #2396ef;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>原型链属性检查器</h1>
        <div class="input-section">
            <label for="object-example-container">示例对象:</label>
            <pre id="object-example-container" contenteditable="true" class="object-example-container">
                <code></code>
            </pre>
            <label for="property-name">输入属性名:</label>
            <input type="text" id="property-name" placeholder="例如: toString">

            <button onclick="checkProperty()">检查</button>
        </div>

        <div id="result" class="result">
            <!-- 结果将在此显示 -->
        </div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script>
    <script>
        const objEle = document.getElementById("object-example-container");
        // 检查对象是否在原型链中有该属性
        const hasPrototypeObject = (obj, name) =>
            !obj.hasOwnProperty(name) && name in obj;

        // 测试对象
        const sampleObject = {
            name: "张三",
            age: 30
        };
        objEle.firstElementChild.textContent = JSON.stringify(sampleObject).trim();
        hljs.highlightElement(objEle);
        // 向原型链中添加方法进行测试
        Object.prototype.toString = function () {
            return `[对象 ${this.constructor.name}]`;
        };

        // 根据用户选择返回不同的对象
        function getValueObject() {
            let value = objEle.value;
            try {
                value = JSON.parse(value);
            } catch (error) {
                return sampleObject;
            }
            return typeof value === 'object' && value !== null ? value : sampleObject;
        }

        function checkProperty() {
            const propertyName = document.getElementById("property-name").value.trim();
            const resultElement = document.getElementById("result");
            const valueObject = getValueObject();

            // 清除之前的结果
            resultElement.classList.remove("success", "error");
            resultElement.innerHTML = '';

            if (propertyName === "") {
                resultElement.classList.add("error");
                resultElement.innerHTML = `<span class="icon icon-error">❌</span>请输入属性名。`;
                return;
            }

            // 检查属性是否存在于原型链中
            if (hasPrototypeObject(valueObject, propertyName)) {
                resultElement.classList.add("success");
                resultElement.innerHTML = `<span class="icon icon-success">✔️</span>${propertyName} 存在于该对象的原型链中。`;
            } else {
                resultElement.classList.add("error");
                resultElement.innerHTML = `<span class="icon icon-error">❌</span>${propertyName} 不存在于该对象的原型链中。`;
            }
        }
    </script>
</body>

</html>